package com.sakura.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sakura.common.R;
import com.sakura.pojo.Employee;
import com.sakura.serivce.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", employee.getUsername());
        // 判断账户是否存在
        Employee databaseEmp = employeeService.getOne(queryWrapper);
        if (databaseEmp == null) {
            return R.error("帐号不存在");
        }
        // 对提交密码进行md5处理
        String submitPassword = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        // 判断密码是否正确
        if (!submitPassword.equals(databaseEmp.getPassword())) {
            return R.error("密码错误");
        }
        // 判断状态是否启用
        if (databaseEmp.getStatus() == 0) {
            return R.error("该员工帐号已禁用");
        }
        session.setAttribute("userInfo", databaseEmp.getId());
        return R.success(databaseEmp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        session.invalidate();
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> addUser(@RequestBody Employee employee, HttpSession session) {
        // 补全其他信息
        // 设置创建和修改时间
        // employee.setCreateTime(LocalDateTime.now());
        // employee.setUpdateTime(LocalDateTime.now());
        //
        // // 设置创建和修改用户
        // employee.setCreateUser((Long) session.getAttribute("userInfo"));
        // employee.setUpdateUser((Long) session.getAttribute("userInfo"));

        // 设置密码加密

        String md5Password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(md5Password);
        employeeService.save(employee);
        log.info("保存成功");
        return R.success("添加成功");
    }

    // 分页查询
    @GetMapping("/page")
    public R<Page<Employee>> selectPage(@RequestParam("page") Integer page,
                                        @RequestParam("pageSize") Integer pageSize,
                                        @RequestParam(required = false ,value = "name") String name) { // 不设置required会与前端请求参数匹配不上
        Page<Employee> employeePage = new Page<>(page, pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.orderByDesc("update_time");
        employeeService.page(employeePage, queryWrapper);
        return R.success(employeePage);
    }

    // 更新用户信息
    @PutMapping
    public R<Object> updateUser(@RequestBody Employee employee, HttpSession session){
        // employee.setUpdateUser((Long) session.getAttribute("userInfo"));
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> editUser(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
